<?php
include("/usr/share/jpgraph/jpgraph.php");
include("/usr/share/jpgraph/jpgraph_line.php");

require_once("config.php");
require_once("CMySQL.php");

define("NB_ANNEE", 4);
define("NB_ANNEE_VIS", NB_ANNEE-1);
define("PERIODE", 12);

$codeClient=$_GET["codeClient"];

function cumulCa($iMax, $caMois)
{
	$ret=0;
	for($i=0;$i<PERIODE;$i++)
	{
		$ret+=$caMois[$iMax-$i];
	}
	
	return $ret;
}

$graph=new Graph(800, 600, "auto");
$graph->title->Set("Chiffre d'affaire sur 12 mois glissant ($codeClient)");
$graph->title->SetFont(FF_VERDANA,FS_NORMAL,12);
$graph->SetScale("textlin");
$graph->yaxis->SetFont(FF_VERDANA,FS_NORMAL,10);
$graph->xaxis->SetFont(FF_VERDANA,FS_NORMAL,10);
$graph->xaxis->SetLabelAngle(50);
$graph->legend->SetFont(FF_VERDANA,FS_NORMAL,10);
$graph->legend->pos(0.02, 0.02);

$dbName="";
if(isset($_GET["dossier"])) {
	$dbName=CMySQL::getInstance()->getDbNameFromDossier($_GET["dossier"]);
}


$sql="SELECT DATE_FORMAT(creation, \"%Y-%m\") AS periode, SUM(prixUnitaireHT*quantite*IF(document=\"A\",-1,1)) AS ca ";
$sql.="FROM teteFactures INNER JOIN ligneFactures ON facture=code ";
$sql.="WHERE DATE_FORMAT(creation, \"%Y-%m\") > DATE_FORMAT(DATE_SUB(CURDATE(), INTERVAL ".(NB_ANNEE-1)." YEAR), \"%Y-%m\") AND codeClient=\"$codeClient\" ";
$sql.="GROUP BY periode ORDER BY periode";

$sqlAFact="SELECT DATE_FORMAT(creation, \"%Y-%m\") AS periode, SUM(prixUnitaireHT*quantite*IF(document=\"A\",-1,1)) AS ca ";
$sqlAFact.="FROM ligneBLs INNER JOIN teteBLs ON bl = code ";
$sqlAFact.="WHERE blFacture(code)=0 AND creation >= DATE_FORMAT(DATE_SUB(CURDATE(), INTERVAL ".(NB_ANNEE-1)." YEAR), \"%Y-01\") AND codeClient=\"$codeClient\" ";
$sqlAFact.="GROUP BY periode";

CMySQL::getInstance()->selectDb($dbName);

$result=CMySQL::getInstance()->execQuery($sql);
$resultAFact=CMySQL::getInstance()->execQuery($sqlAFact);

date_default_timezone_set("Europe/Paris");
$today=getdate();
$periodeMax=$today["year"]."-".str_pad($today["mon"], 2, "0", STR_PAD_LEFT);

$data=array();
$dataAFact=array();

$row=mysql_fetch_array($result);
$rowAFact=mysql_fetch_array($resultAFact);

$annee=$today["year"]-NB_ANNEE+1;
$mois=$today["mon"]+1;
if($mois == 13)
{
	$annee++;
	$mois=1;
}
$i=0;
$caBase=0;
do
{
	$periode=$annee."-".str_pad($mois, 2, "0", STR_PAD_LEFT);
	$value=0;
	$valueAFact=0;
	if($row)
	{
		if($periode == $row["periode"])
		{
			$value=$row["ca"]*1;
			$row=mysql_fetch_array($result);
		}	
	}
	if($rowAFact)
	{
		if($periode == $rowAFact["periode"])
		{
			$valueAFact=$rowAFact["ca"]*1;
			$rowAFact=mysql_fetch_array($resultAFact);
		}
	}
	
	$caMois[$i]=$value;
	$caMoisAFact[$i]=$value+$valueAFact;
	
	if($i >= PERIODE)
	{
		$xData[$i-PERIODE]=$periode;
		
		$data[$i-PERIODE]=cumulCa($i, $caMois);	
		$dataAFact[$i-PERIODE]=cumulCa($i, $caMoisAFact);	
	}

	$mois++;
	if($mois == 13)
	{
		$annee++;
		$mois=1;
	}
	$i++;
}while($periode < $periodeMax);

$lPlot=new LinePlot($data);
$lPlot->SetColor("blue");
$lPlot->SetLegend("CA facturé");

$lPlotAFact=new LinePlot($dataAFact);
$lPlotAFact->SetColor("red");
$lPlotAFact->SetLegend("CA a facturer");

$graph->xaxis->SetTickLabels($xData);

$graph->Add($lPlotAFact);
$graph->Add($lPlot);

/*echo "<pre>";
print_r($caMois);
print_r($caMoisAFact);
print_r($data);
print_r($dataAFact);
echo "</pre>";*/

$graph->stroke();
?>
